<?php    
	class conexion
	{
		var $host;
		var $bd;
		var $user;
		var $pass;
		var $link;

		function conexion()
		{
			$this->host="localhost";
			$this->bd="redsocial";
			$this->user="root";
			$this->pass="root";
		}
        function numrows( $result )
        {
             return mysql_num_rows( $result );
        }
  
        function  fetch( $result )
        {
            return mysql_fetch_row( $result );
        } 
        function cambiarBD($bdName)
        {
            if(!(mysql_select_db($bdName,$this->link)))
			{
				echo "no se pudo seleccionar la base de datos";
                return 2;
			}
			else
			{
				return $this->link;
			}   
        }
		function conectar()
		{
			if(!($this->link=mysql_connect($this->host,$this->user,$this->pass)))
			{
			    echo"no se pudo conectar al servidor";
                return 1;
			}
			else
			{
				if(!(mysql_select_db($this->bd,$this->link)))
				{
					echo "no se pudo seleccionar la base de datos";
                    return 2;
				}
				else
				{
					return $this->link;
				}
			}
		}
		function query($sql)
		{
		   
			$this->conectar();
            mysql_query("SET NAMES utf8");
          	$this->result=mysql_query($sql);            
            mysql_close();                                    
			return $this->result;
		}
        function executeQuery($sql)
		{
		   
			$this->conectar();
            mysql_query("SET NAMES utf8");
          	$this->result=mysql_query($sql);            
            mysql_close();                                    
			return $this->result;
		}



		function selectOption($sql,$name,$type,$value,$class)
		{
		  
			$this->conectar();
            
            $consulta= $this->query($sql);         
            $select="";
            $select="<select name='$name' id='$name' class='validate[required]' >";
            if(mysql_num_rows($consulta)>0)
            {
                
                
                        
                if( $type == 'todos')
                $select.="<option value='todos' selected >TODOS</option>";
                if( $type == 'seleccione')
                $select.="<option value='-1' selected >Seleccione</option>";
                
                while( ($fila = mysql_fetch_row($consulta) ) )
    			{
                     $fila[1] = strtoupper($fila[1]);
                     
                    if( $value == $fila[0])
    				$select.="<option value='$fila[0]' selected >$fila[1]</option>";
                    else
                    $select.="<option value='$fila[0]'>$fila[1]</option>";
    			}
            }
            
            $select.="</select>";
            
			return $select;
		}
        function selectOptionGroup($sqlOrigen,$sqlDestino,$name)
		{
			$this->conectar();
            $consultaComunas=mysql_query($sqlOrigen);         
            
            
            $select="";            
            if(mysql_num_rows($consultaComunas)>0)
            {            
               $select="<select name='$name' id='$name' >";
                
                while( ($comunas = mysql_fetch_row($consultaComunas) ) )
			    {
    			    
                     $consultaBarrios=mysql_query($sqlDestino." ".$comunas[0]);
                    
                    
                    if(mysql_num_rows($consultaBarrios)>0)
                    {
                       $select="<select name='$name' id='$name' ><option value='-1'>Seleccione...</option>";
                 
                       $select.="<optgroup label='$comunas[1]'>";
                       while( $barrios = mysql_fetch_row($consultaBarrios) )
                       {
                         $barrios[1] = strtoupper($barrios[1]);
                         $select.="<option value='$barrios[0]'>$barrios[1]</option>";       
                       }
                       $select.="</optgroup>";    
                    }                    
    			}
            }
            else
            {
                $select.="<select class='$class'><option value='-1'>No hay datos</option>";
            }
            $select.="</select>";
            
			return $select;
		}


	}
?>
